
function numbersOnly(string) {
    if (string.search(/^[0-9\.]*$/) != -1)
         return true;
     else
         return false;
}


function GGCalc() {
	 //this.popup = null;
	 this.a1 = null;
	 this.retail=null;
	 this.ggans=null;
	
	
	this.calc = function(retail, finalprice) {
	//alert('calc function');
	//alert(retail + ' ' + finalprice);
	if (retail == null || finalprice == null) return null;
	retail = retail.replace(/\$/g, "");
	retail = parseFloat(retail);
	finalprice = parseFloat(finalprice);
	if (isNaN(retail) || isNaN(finalprice)) return null;
	//alert(retail + ' ' + finalprice);
	if (finalprice > retail) {
		 var ggwarn1 = SS_getStringTableText('ggcalc_warn1');
		SS_GGCalcWindow.alert(ggwarn1);
		return "";
	}
	var temp = retail-finalprice;
	//alert('sub temp ' + temp);
	temp = temp/retail;
	//alert('div temp ' + temp);
	temp = temp*100;
	//round to two decimals
	temp=Math.round(temp*100)/100;
	
	return temp;
	
	
}//end calc


	this.showcalc = function(jfretail, maindoc, guruinput){

	

	
	//  _SSGGCalcWindow=window.open('chrome://swaksmack/content/ggcalc.html','SwakSmack Guru Calculator','height=400,width=500');
	 

	  



this.retail = jfretail;
		var handle = this;
  
//	_SSGGCalcWindow.focus();
//var wtf = _SSGGCalcWindow.opener;
 // alert(_SSGGCalcWindow.opener);
 // SS_GGCalcWindow.alert('popup jfretail ' + jfretail);
  var gurucalcans = SS_GGCalcWindow.document.getElementById('gganswer');
//  handle.popup.alert('doc gurucalcans ' + gurucalcans);
  		//store reference to stuff

  this.ggans=gurucalcans;
 // alert('ctor this.ggans ' + this.ggans);
  
  var product = SS_GGCalcWindow.document.getElementById('ggproductprice');
  product.textContent=this.retail;
  //_SSGGCalcWindow.alert(this.retail + ' product text ' +  product.textContent);
  
  var gginput = SS_GGCalcWindow.document.getElementById('gginput'); 
  var inputform = SS_GGCalcWindow.document.getElementById('ggsellprice'); 
  var ggcomment = SS_GGCalcWindow.document.getElementById('ggcomment');

  inputform.addEventListener('submit',function(event) {
  	//handle.popup.alert(' EL submit  ' + event.originalTarget.elements[0].value);
  	var userguess = event.originalTarget.elements[0].value;
  	    //var gurudiv = doc.getElementById('guruHasNick');
    		var e1=maindoc.getElementsByName('user_guess');
    		var guruinput;
    		for (var i=0;i<e1.length;i++) {
    		 	if (e1[i].name == 'user_guess') {
    		 		guruinput = e1[i];
    		 		//handle.popup.alert('gg user guess found');
    		 		break;
    			}
    		}
    		if (guruinput != null) {
    		//	alert('gg guruinput FOUND' );
    		//handle.popup.alert('submit value ' + userguess);
    			///var result = calc(handle.retail,userguess);
    			var result = handle.calc(handle.retail,userguess);
    			if (result == null) {
    				SS_GGCalcWindow.alert('Guru Guess Calculator missing retail or user final price.  Sorry you are on your own...');
    				return;
    			}
    			//this.calc(retail,event.target.value);
    			guruinput.value=result;
    		}
    		else {
    			//error
    			SS_GGCalcWindow.alert('guruinput NOT found');
    		}
  	
  	}, true);
  	
  	gginput.addEventListener('keyup',function(event) {
  		var svalue=event.target.value;
  		if (svalue==null || svalue=='') {
  			handle.ggans.textContent='';
  			ggcomment.textContent='';
  		}
  		else if (!numbersOnly(svalue)) {
  		gginput.value=svalue.substring(0,svalue.length-1);
  	  ggcomment.textContent='';
  		//handle.popup.alert('numbers only');

  	}
  	else {
  		  var e1=maindoc.getElementsByName('user_guess');
    		var guruinput;
    		for (var i=0;i<e1.length;i++) {
    		 	if (e1[i].name == 'user_guess') {
    		 		guruinput = e1[i];
    		 		//handle.popup.alert('popup gg user guess found');
    		 		break;
    			}
    		}
    		if (guruinput != null) {
    			//handle.popup.alert('gg guruinput FOUND' );
    			var result = handle.calc(handle.retail,event.target.value);
    			if (result == null) {
    				ggcomment.textContent='';
    				//alert('Guru Guess Calculator missing retail or user final price.  Sorry you are on your own...');
    				return;
    			}

    			handle.ggans.textContent=result;
    			if(result <15) {
    				ggcomment.textContent=SS_getStringTableText('ggcalc_comment0');
    			}
    			else if (result <55 && result>=15) {
    				ggcomment.textContent=SS_getStringTableText('ggcalc_comment15');
    			}
    			else if (result <80 && result>=55) {
    				ggcomment.textContent=SS_getStringTableText('ggcalc_comment55');
    			}
    			else if (result >=80) {
    				ggcomment.textContent=SS_getStringTableText('ggcalc_comment80');
    			}
    			else {
    				//handle.popup.alert('unhandled result ' + result);
    				ggcomment.textContent=SS_getStringTableText('ggcalc_unexpected1');
    			}

    		}
    		else {
    			//error
    			//alert('guruinput NOT found');
    		}
  	}
  	}, true);
  	


} //end show
	

}